package com.linzp.bishe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linzp.bishe.common.Result;
import com.linzp.bishe.common.SaveAddress;
import com.linzp.bishe.entity.Publications;
import com.linzp.bishe.mapper.PublicationsMapper;
import com.linzp.bishe.model.PublicationsModel;
import com.linzp.bishe.service.ParticipantsService;
import com.linzp.bishe.service.PublicationsService;
import com.linzp.bishe.utils.DealFile;
import com.linzp.bishe.utils.TableName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林泽鹏
 * @since 2021-11-01
 */
@Service
public class PublicationsServiceImpl extends ServiceImpl<PublicationsMapper, Publications> implements PublicationsService {

	@Autowired
	private SaveAddress saveAddress;

	@Autowired
	private PublicationsMapper publicationsMapper;

	@Autowired
	private ParticipantsService participantsService;

	@Override
	public String saveMultipartFile(MultipartFile file) {
		return DealFile.saveMultipartFile(file, saveAddress.getAllAddress() + saveAddress.getCache());
	}

	@Override
	public void saveCacheToFile(String name) {
		DealFile.saveCacheToFile(saveAddress.getAllAddress() + saveAddress.getCache(),
				saveAddress.getAllAddress() + saveAddress.getPublications(), name);
		DealFile.DeleteFolder(saveAddress.getAllAddress() + saveAddress.getCache() + "/" + name);
	}

	/**
	 * @Description 通过id找到特定用户信息
	 * @Param [success, id, current, page, searchPublicationName, searchStatus]
	 * @return void
	 */
	@Override
	public void getByIdAllPublications(Result success, Integer id, Integer current,
	                                   Integer page, String searchPublicationName, String searchStatus) {
		if (searchPublicationName != null & !searchPublicationName.isEmpty()) {
			searchPublicationName = '%' + searchPublicationName + '%';
		}
		success.put("tableData",publicationsMapper.getByIdAllPublications(id, TableName.PUBLICATIONS,
				searchPublicationName, searchStatus,(current - 1) * page,page));
		success.put("total",publicationsMapper.getByIdAllPublicationsCount(id, TableName.PUBLICATIONS,
				searchPublicationName, searchStatus));
		success.put("loadAddress",saveAddress.getAccessPath()+saveAddress.getPublications());
	}

	@Override
	public String updateMultipartFile(MultipartFile file, String fileName) {
		String name = DealFile.upDateMultipartFile(file, saveAddress.getAllAddress() + saveAddress.getPublications(), fileName);
		if(!name.equals(fileName)){
			DealFile.DeleteFolder(saveAddress.getAllAddress() + saveAddress.getPublications() + "/" +fileName);
		}
		return name;
	}

	/**
	 * @Description 删除出版刊物信息
	 * @return void
	 * @Param [publicationsModel]
	 */
	@Override
	public void deletePublications(PublicationsModel publicationsModel) {
		Publications publications = publicationsModel.getPublications();
		this.removeById(publications.getId());

		String materials = publications.getSupportingMaterial();
		if (materials !=null & !materials.isEmpty()){
			DealFile.DeleteFolder(saveAddress.getAllAddress() + saveAddress.getPublications() + "/" + materials);
		}

		participantsService.removeListById(publicationsModel.getParticipants());
	}

	/**
	 * @Description 处理状态
	 * @Param [id, status]
	 * @return void
	 */
	@Override
	public void dealStatus(Integer id, String status) {
		UpdateWrapper<Publications> wrapper = new UpdateWrapper<>();
		wrapper.eq("id",id);
		wrapper.set("status",status);
		this.update(wrapper);
	}

	@Override
	public boolean cheakRepeat(String element1) {
		QueryWrapper<Publications> wrapper = new QueryWrapper<>();
		wrapper.eq("publication_name",element1);
		return this.getOne(wrapper,false)!=null ? true:false;
	}
}
